package view;

import control.Cont_conexao;
import java.awt.Dimension;
import static java.lang.Thread.sleep;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import model.Log_sistema;

public class gui_gerencial extends javax.swing.JFrame {
	
//=========================================================================================
//	VARIÁVEIS GLOBAIS
//=========================================================================================
	
	EntityManager gerenciador;
	Query consulta;
	Cont_conexao conexao = new Cont_conexao();
	Log_sistema log = new Log_sistema();
	public static String usuarioAtual;
	
//=========================================================================================

	public gui_gerencial() {
		initComponents();
		this.setExtendedState(MAXIMIZED_BOTH);
		this.InsereDataHora();
	}//CONSTRUTOR

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnl_principal = new javax.swing.JPanel();
        pnl_rodape = new javax.swing.JPanel();
        lbl_codi = new javax.swing.JLabel();
        lbl_data = new javax.swing.JLabel();
        lbl_hora = new javax.swing.JLabel();
        pnl_interno = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        pnl_principal.setBackground(new java.awt.Color(255, 255, 255));

        pnl_rodape.setBackground(new java.awt.Color(255, 255, 255));
        pnl_rodape.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        lbl_codi.setText("USUÁRIO");

        lbl_data.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbl_data.setText("DATA");

        lbl_hora.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbl_hora.setText("HORA");

        javax.swing.GroupLayout pnl_rodapeLayout = new javax.swing.GroupLayout(pnl_rodape);
        pnl_rodape.setLayout(pnl_rodapeLayout);
        pnl_rodapeLayout.setHorizontalGroup(
            pnl_rodapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_rodapeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbl_codi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(132, 132, 132)
                .addComponent(lbl_data, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbl_hora, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnl_rodapeLayout.setVerticalGroup(
            pnl_rodapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbl_codi, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
            .addComponent(lbl_data, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(lbl_hora, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pnl_interno.setBackground(new java.awt.Color(255, 255, 255));
        pnl_interno.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        javax.swing.GroupLayout pnl_internoLayout = new javax.swing.GroupLayout(pnl_interno);
        pnl_interno.setLayout(pnl_internoLayout);
        pnl_internoLayout.setHorizontalGroup(
            pnl_internoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 655, Short.MAX_VALUE)
        );
        pnl_internoLayout.setVerticalGroup(
            pnl_internoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout pnl_principalLayout = new javax.swing.GroupLayout(pnl_principal);
        pnl_principal.setLayout(pnl_principalLayout);
        pnl_principalLayout.setHorizontalGroup(
            pnl_principalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnl_principalLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(pnl_principalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnl_rodape, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnl_interno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(5, 5, 5))
        );
        pnl_principalLayout.setVerticalGroup(
            pnl_principalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnl_principalLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(pnl_interno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnl_rodape, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5))
        );

        jMenu1.setMnemonic('c');
        jMenu1.setText("Cadastros");

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_1, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem4.setText("Categoria");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_2, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem5.setText("Produto");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_3, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem6.setText("Fornecedor");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem7.setText("Usuário");
        jMenu1.add(jMenuItem7);

        jMenuItem9.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_5, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem9.setText("Operador");
        jMenu1.add(jMenuItem9);

        jMenuBar1.add(jMenu1);

        jMenu2.setMnemonic('r');
        jMenu2.setText("Relatórios");
        jMenuBar1.add(jMenu2);

        jMenu3.setMnemonic('m');
        jMenu3.setText("Movimentação");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItem1.setText("Abertura de caixa");
        jMenu3.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem2.setText("Logoff");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem3.setText("Encerrar o sistema");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuBar1.add(jMenu3);

        jMenu4.setText("Configurações");

        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem8.setText("Loja");
        jMenu4.add(jMenuItem8);

        jMenuItem10.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem10.setText("Sistema");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem10);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnl_principal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnl_principal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
		gui_categoria novo = new gui_categoria();
		pnl_interno.add(novo);
		this.CentralizaForm(novo);
		novo.setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
		//Criar a mensagem sera exibida
		JLabel label = new JLabel("Digite a senha gerencial:");
		//criar o componente grafico que recebera o que for digitado
		JPasswordField password = new JPasswordField();
		//Exibir a janela para o usuario
		JOptionPane.showConfirmDialog(null,
		new Object[]{label, password}, "Senha:",
		JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
		String senha = password.getText();
		
		if(senha.equals("1")){
			System.exit(0);
		}//IF
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
		new gui_login().setVisible(true);
		this.dispose();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
		gui_config novo = new gui_config();
		pnl_interno.add(novo);
		this.CentralizaForm(novo);
		novo.setVisible(true);
    }//GEN-LAST:event_jMenuItem10ActionPerformed

	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Metal".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(gui_gerencial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(gui_gerencial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(gui_gerencial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(gui_gerencial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
        //</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new gui_gerencial().setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JLabel lbl_codi;
    private javax.swing.JLabel lbl_data;
    private javax.swing.JLabel lbl_hora;
    private javax.swing.JPanel pnl_interno;
    private javax.swing.JPanel pnl_principal;
    private javax.swing.JPanel pnl_rodape;
    // End of variables declaration//GEN-END:variables

	//CENTRALIZA INTERNAL FRAME
         private void CentralizaForm(JInternalFrame frame) {
                  Dimension desktopSize = pnl_interno.getSize();//Captura a altura e largura do painel
                  Dimension jInternalFrameSize = frame.getSize();//Captura a alltura e largura do internal frame
                  frame.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                  (desktopSize.height - jInternalFrameSize.height) / 2);
         }//CENTRALIZA INTERNAL FRAME

	//CONFIGURAÇÕES
	public void Configura(String usuario){
		
		this.lbl_codi.setText("CÓDIGO DO USUÁRIO: ");
		
	}//DADOS NO RODAPÉ
		 
         //DATA E HORA
         public void InsereDataHora(){
    
		//NOVA THREAD
		new Thread() {
			
			@Override
			public void run() {

				for(;;) {

					//Instancia o calendario gregoriano
					GregorianCalendar Calendar = new GregorianCalendar();
					//Captura o dia
					int dia = Calendar.get(GregorianCalendar.DAY_OF_MONTH);
					Calendar data = Calendar.getInstance();

					//Captura o mês
					String mes="";
					switch(data.get(Calendar.MONTH)+1) {
						case 1:mes="Janeiro"; break;
						case 2:mes="Fevereiro‎"; break;
						case 3:mes="Março‎"; break;
						case 4:mes="Abril‎"; break;
						case 5:mes="Maio"; break;
						case 6:mes="Junho"; break;
						case 7:mes="Julho"; break;
						case 8:mes="Agosto"; break;
						case 9:mes="Setembro"; break;  
						case 10:mes="Outubro"; break; 
						case 11:mes="Novembro"; break; 
						case 12:mes="Dezembro"; break;
						default:mes="Mês Inválido"; break;
					}//SWITCH

					//Captura o ano
					int ano = Calendar.get(GregorianCalendar.YEAR);

					//Finaliza com a data atual
					lbl_data.setText(dia+"/"+mes+"/"+ano);

					//Define o relógio com a hora atual
					int hora=Calendar.get(GregorianCalendar.HOUR_OF_DAY);//HORA
					int min=Calendar.get(GregorianCalendar.MINUTE);//MINUTOS
					int seg=Calendar.get(GregorianCalendar.SECOND);//SEGUNDOS

					//Variaveis que serão exibidas 
					final String horaString;
					final String minString;
					final String segString;

					//Condição para aplicar um 0 caso a hora seja de apenas 1 digito
					if(hora<10) {
					horaString="0"+hora;
					} else {
					   horaString=""+hora;
					}//IF

					//Condição para aplicar um 0 caso os minutos sejam de apenas 1 digito
					if(min<10) {
						minString="0"+min;
					} else {
					   minString=""+min;
					}//IF

					//Condição para aplicar um 0 caso os segundos sejam de apenas 1 digito
					if(seg<10) {
						segString="0"+seg;
					} else {
					   segString=""+seg;
					}//IF

					//Finaliza com o relógio que será exibido
					lbl_hora.setText(horaString+":"+minString+":"+segString);

					//Define o tempo de atualização do label
					try {
						  sleep(1000);
						}//TRY
					catch (InterruptedException ex) {
					 System.out.println("Ocorreu um erro");
					}//CHATCH  

				}//FOR

			}//RUN

		}//THREAD
		.start();

	}//DATA HORA

}//CLASS
